package com.can.lesson01;

import java.sql.*;

/**
 * 第一个JDBC程序
 */
public class JdbcFirstDemo {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");//固定写法，加载驱动
        //2.用户信息和url,useSSL使用安全的连接
        String url = "jdbc:mysql://localhost:3306/kuangshenjdbc?useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String username = "root";
        String password = "root";
        //3.连接成功，返回数据库对象
        Connection connection = DriverManager.getConnection(url,username,password);
        //默认会开启事务
        connection.setAutoCommit(false);//关闭事务
        //4.执行SQL的对象
        Statement statement = connection.createStatement();
        //5.执行SQL的对象， 去执行SQL，可能存在结果，查询返回结果
        ResultSet resultSet = statement.executeQuery("SELECT * FROM users");    //返回结果集
        PreparedStatement r1 = connection.prepareStatement("update  users set password='123' where name ='can1' ");
        int i = r1.executeUpdate();
        System.out.println(i);
        //提交事务
        connection.commit();
        //归滚
//        connection.rollback();
        while (resultSet.next()){
            System.out.println("id==>"+resultSet.getObject("id"));
            System.out.println("name==>"+resultSet.getObject("name"));
            System.out.println("password==>"+resultSet.getObject("password"));
            System.out.println("email==>"+resultSet.getObject("email"));
            System.out.println("birthday==>"+resultSet.getObject("birthday"));
        }
        //6.释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
